package com.wanxi.service.Impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.wanxi.dao.Impl.ProductTypeDaoImpl;
import com.wanxi.dao.ProductTypeDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.Image;
import com.wanxi.model.ProductType;
import com.wanxi.service.ProductTypeService;
import com.wanxi.util.JSONCopy;
import com.wanxi.util.RedisTool;
import com.wanxi.vo.ProductTypeVO;

import java.util.Iterator;
import java.util.List;

public class ProductTypeServiceImpl implements ProductTypeService {
    ProductTypeDao productTypeDao = new ProductTypeDaoImpl();

    @Override
    public ResultDTO findAllType() {
        List<ProductType> list = productTypeDao.findAllType();
        if (list.size() > 0) {
            return ResultDTO.getDTO(list.size(), list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }

    @Override
    public ResultDTO findProductTypeById(ProductTypeVO productTypeVO) {
        ProductType productType = JSONCopy.copy(productTypeVO, ProductType.class);
        ProductType pt = productTypeDao.findProductTypeById(productType);
        if (pt != null) {
            return ResultDTO.getDTO(1, pt);
        }
        return ResultDTO.builder().count(0).msg("error").code(1).build();
    }

    @Override
    public ResultDTO updateEnable(ProductTypeVO productTypeVO) {
        ProductType productType = JSONCopy.copy(productTypeVO, ProductType.class);
        int i = productTypeDao.updateEnable(productType);
        if (i == 1) {
            RedisTool.deleteToRedis("productType");
            return ResultDTO.getDTO(i, productTypeVO);
        }
        return ResultDTO.builder().data(productTypeVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO addProductType(ProductTypeVO productTypeVO) {
        ProductType productType = JSONCopy.copy(productTypeVO, ProductType.class);
        int i = productTypeDao.addProductType(productType);
        if (i == 1) {
            RedisTool.deleteToRedis("productType");
            return ResultDTO.getDTO(i, productTypeVO);
        }
        return ResultDTO.builder().data(productTypeVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO deleteProductType(ProductTypeVO productTypeVO) {
        ProductType productType = JSONCopy.copy(productTypeVO, ProductType.class);
        int i = productTypeDao.deleteProductType(productType);
        if (i == 1) {
            RedisTool.deleteToRedis("productType");
            return ResultDTO.getDTO(i, productTypeVO);
        }
        return ResultDTO.builder().data(productTypeVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO updateProductType(ProductTypeVO productTypeVO) {
        ProductType productType = JSONCopy.copy(productTypeVO, ProductType.class);
        int i = productTypeDao.updateProductType(productType);
        if (i == 1) {
            RedisTool.deleteToRedis("productType");
            return ResultDTO.getDTO(i, productTypeVO);
        }
        return ResultDTO.builder().data(productTypeVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO findType() {
        List<String> redisProductType = RedisTool.findToRedis("productType");
//        判断redis中是否有数据
        if (redisProductType != null) {
//            返回redis中的数据,通过fastJson转换
            List<ProductType> list = JSON.parseObject(redisProductType.toString(), new TypeReference<List<ProductType>>() {
            });
            return ResultDTO.getDTO(list.size(), list);
        }
//        没有则从数据库中找数据
        List<ProductType> list = productTypeDao.findAllType();
        if (list.size() > 0) {
            Iterator<ProductType> iterator = list.iterator();
            while (iterator.hasNext()) {
                ProductType pt = iterator.next();
                if (pt.getEnable() == 0)
                    iterator.remove();
                else
//                    将查到的数据放入redis
                    RedisTool.putToRedis("productType", JSON.toJSONString(pt));
            }
            return ResultDTO.getDTO(list.size(), list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }
}
